package com.pattern.design.designpattern;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.pattern.design.designpattern.AbstractFactory.CarFactory;
import com.pattern.design.designpattern.AbstractFactory.Q3Factory;
import com.pattern.design.designpattern.AbstractFactory.Q7Factory;
import com.pattern.design.designpattern.Aidl.ServiceTest;
import com.pattern.design.designpattern.Baozhuang.Boy;
import com.pattern.design.designpattern.Baozhuang.CheapCloth;
import com.pattern.design.designpattern.Baozhuang.GoodCloth;
import com.pattern.design.designpattern.Bridge.LargeCoffee;
import com.pattern.design.designpattern.Bridge.Milk;
import com.pattern.design.designpattern.Bridge.SmallCoffee;
import com.pattern.design.designpattern.Bridge.Sugar;
import com.pattern.design.designpattern.Builder.MacbookBuilder;
import com.pattern.design.designpattern.Celve.PriceCalculator;
import com.pattern.design.designpattern.Celve.TexiPrice;
import com.pattern.design.designpattern.Command.Command1;
import com.pattern.design.designpattern.Command.Command2;
import com.pattern.design.designpattern.Command.Command3;
import com.pattern.design.designpattern.Command.Receiver;
import com.pattern.design.designpattern.Compose.CompositeBranch;
import com.pattern.design.designpattern.Compose.CompositeLeaf;
import com.pattern.design.designpattern.Compose.CompositeRoot;
import com.pattern.design.designpattern.Diedaiqi.CompanyA;
import com.pattern.design.designpattern.Diedaiqi.CompanyB;
import com.pattern.design.designpattern.Factory.AudiCarFactory;
import com.pattern.design.designpattern.Factory.AudiFactory;
import com.pattern.design.designpattern.Factory.AudiQ3;
import com.pattern.design.designpattern.Factory.AudiQ5;
import com.pattern.design.designpattern.Factory.AudiQ7;
import com.pattern.design.designpattern.Moban.CoderComputer;
import com.pattern.design.designpattern.Moban.WarComputer;
import com.pattern.design.designpattern.Note.Call4Duty;
import com.pattern.design.designpattern.Note.User;
import com.pattern.design.designpattern.Proxy.Cici;
import com.pattern.design.designpattern.Proxy.Lawyer;
import com.pattern.design.designpattern.Status.TvController;
import com.pattern.design.designpattern.Watcher.BeWacher;
import com.pattern.design.designpattern.Watcher.Watcher;
import com.pattern.design.designpattern.Xiangyuan.Ticket;
import com.pattern.design.designpattern.Xiangyuan.TicketFactory;
import com.pattern.design.designpattern.Yuanxing.WordDocument;
import com.pattern.design.designpattern.Zeren.Leader1;
import com.pattern.design.designpattern.Zeren.Leader2;
import com.pattern.design.designpattern.Zeren.Leader3;
import com.pattern.design.designpattern.databind.MyViewModel;
import com.pattern.design.designpattern.databind.UserAdapter;
import com.pattern.design.designpattern.databind.UserBean;
import com.pattern.design.designpattern.databinding.ActivityMainBinding;
import com.pattern.design.designpattern.databinding.UserItemBinding;

import java.util.ArrayList;
import java.util.List;

import aidl.MyAIDLService;

/**
 * The type Main activity.
 */
public class MainActivity extends AppCompatActivity {
    ServiceTest.MyBinder myBinder;
    private MyAIDLService myAIDLService;


    private ServiceConnection connectionSelf = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (ServiceTest.MyBinder) service;
            myBinder.getString(); //获取到getString方法
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    private ServiceConnection connectionAidl = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myAIDLService = MyAIDLService.Stub.asInterface(service);
            try {
                String str = myAIDLService.getString();
                Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            myAIDLService = null;
        }
    };


    // data binding
    MyViewModel myViewModel;
    ActivityMainBinding binding;
    List mItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        // 初始化数据
        mItems = new ArrayList(20);
        for (int i = 0; i < 15; i++) {
            UserBean u = new UserBean();
            u.setId(i + "");
            u.setName("zhangphil @" + i);
            u.setBlog("blog.csdn.net/zhangphil @" + i);
            mItems.add(u);
        }

        // data binding
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        myViewModel = ViewModelProviders.of(this).get(MyViewModel.class);

        //databinding 后加上
        binding.setData(myViewModel);


        binding.recyclerview.setLayoutManager(
                new LinearLayoutManager(this,
                        LinearLayoutManager.VERTICAL, false));


//        ItemAdapter mItemAdapter = new ItemAdapter(this,mItems);
//        binding.recyclerview.setAdapter(mItemAdapter);

        UserAdapter adapter = new UserAdapter(this, new UserAdapter.BindingItemListen<UserBean, UserItemBinding>() {
            @Override
            public void onItemClick(UserItemBinding binding, UserBean userBean, int i) {
                Toast.makeText(getApplicationContext(), userBean.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        binding.recyclerview.setAdapter(adapter);
        adapter.refreshData(mItems);




//        binding.setUserlist(mItems);
        binding.setLifecycleOwner(this);


        //databinding 后不用了
        /*myViewModel.getNumber().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer integer) {
                binding.hello.setText(String.valueOf(integer));
            }
        });*/
        //databinding 后不用了
        /*binding.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myViewModel.add();
            }
        });*/
    }




    /**
     * System out.
     *
     * @param s the s
     */
    public static void systemOut(String s) {
        System.out.println(s);
    }

    private void TestBuilder() {
        systemOut(new MacbookBuilder().setBoard("intel").setDisplay("retina").setOS().creat().toString());
    }

    private void TestYuanxing() {
        WordDocument wordDocument = new WordDocument();
        wordDocument.setTest("1");
        wordDocument.addImages("image1");
        wordDocument.addImages("image2");
        wordDocument.addImages("image3");
        wordDocument.showDocument();
        WordDocument wordDocument2 = wordDocument.clone();
        wordDocument2.showDocument();
        wordDocument2.setTest("2");
        wordDocument.showDocument();
        wordDocument2.showDocument();
    }

    private void TestFactory() {
        AudiFactory audiFactory = new AudiCarFactory();
        AudiQ3 audiQ3 = audiFactory.creatAudiCar(AudiQ3.class);
        AudiQ5 audiQ5 = audiFactory.creatAudiCar(AudiQ5.class);
        AudiQ7 audiQ7 = audiFactory.creatAudiCar(AudiQ7.class);

        audiQ3.drive();
        audiQ3.selfNavigation();

        audiQ5.drive();
        audiQ5.selfNavigation();

        audiQ7.drive();
        audiQ7.selfNavigation();
    }

    private void TestAbstractFactory() {
        CarFactory factoryQ3 = new Q3Factory();
        factoryQ3.createTire().tire();
        factoryQ3.createEngine().engine();
        factoryQ3.createBreak().brake();
        CarFactory factoryQ7 = new Q7Factory();
        factoryQ7.createTire().tire();
        factoryQ7.createEngine().engine();
        factoryQ7.createBreak().brake();
    }

    private void TestCelve() {
        PriceCalculator priceCalculator = new PriceCalculator();
        priceCalculator.setPrice(new TexiPrice());
        int money = priceCalculator.makePrice(6);
        systemOut(money + "");
    }

    private void TestStatus() {
        TvController tvController = new TvController();
        tvController.powerOn();
        tvController.nextChannel();
        tvController.turnUp();
        tvController.powerOff();
        tvController.turnDown();
    }

    private void TestZeren() {
        Leader1 leader1 = new Leader1();
        Leader2 leader2 = new Leader2();
        Leader3 leader3 = new Leader3();
        leader1.nextHandler = leader2;
        leader2.nextHandler = leader3;
        leader1.handleRequest(250);
    }

    private void TestCommand() {
        Receiver receiver = new Receiver();
        Command1 command1 = new Command1(receiver);
        Command2 command2 = new Command2(receiver);
        Command3 command3 = new Command3(receiver);

        command1.want2do();
        command2.want2do();
        command3.want2do();
    }

    private void TestWacher() {
        BeWacher beWacher = new BeWacher();
        Watcher watcher1 = new Watcher("111");
        Watcher watcher2 = new Watcher("222");
        Watcher watcher3 = new Watcher("333");
        beWacher.addObserver(watcher1);
        beWacher.addObserver(watcher2);
        beWacher.addObserver(watcher3);
        beWacher.publishSth("hello_bitches");
    }

    private void TestNote() {
        Call4Duty call4Duty = new Call4Duty();
        User user = new User();

        call4Duty.play();
        user.save(call4Duty.creatNote());
        call4Duty.quit();

        Call4Duty call4DutyNew = new Call4Duty();
        call4DutyNew.restore(user.load());

    }

    private void TestDiedaiqi() {
        CompanyA companyA = new CompanyA();
        CompanyB companyB = new CompanyB();
        while (companyA.diedaiqi().hasNext()) {
            MainActivity.systemOut(companyA.diedaiqi().getEmployee().toString());
        }
        while (companyB.diedaiqi().hasNext()) {
            MainActivity.systemOut(companyB.diedaiqi().getEmployee().toString());
        }
    }

    private void TestMoban() {
        CoderComputer coderComputer = new CoderComputer();
        WarComputer warComputer = new WarComputer();
        coderComputer.start();
        warComputer.start();
        coderComputer.start();
        warComputer.start();
    }

    private void TestProxy() {
        Lawyer lawyer = new Lawyer(new Cici());
        lawyer.submit();
        lawyer.juzheng();
        lawyer.bianhu();
        lawyer.finish();
    }

    private void TestComposite() {
        CompositeRoot compositeRoot = new CompositeRoot("root");
        CompositeBranch compositeBranch1 = new CompositeBranch("branch1");
        CompositeBranch compositeBranch2 = new CompositeBranch("branch2");
        CompositeLeaf compositeLeaf1 = new CompositeLeaf("leaf1");
        CompositeLeaf compositeLeaf2 = new CompositeLeaf("leaf2");
        compositeBranch1.addChild(compositeLeaf1);
        compositeBranch2.addChild(compositeLeaf2);
        compositeRoot.addChild(compositeBranch1);
        compositeRoot.addChild(compositeBranch2);
        compositeRoot.doSth();
    }

    private void TestBaozhuang() {
        Boy boy = new Boy();
        CheapCloth cheapCloth = new CheapCloth(boy);
        GoodCloth goodCloth = new GoodCloth(boy);
        cheapCloth.dressed();
        goodCloth.dressed();
    }

    private void TestXiangyuan() {
        Ticket ticket1 = TicketFactory.getTicket("xian", "beijig");
        Ticket ticket2 = TicketFactory.getTicket("xian", "shijiazhuang");
        ticket1.showInfo("34A");
        ticket1.showInfo("44D");
    }

    private void TestBridge() {
        Milk milk = new Milk();
        Sugar sugar = new Sugar();
        LargeCoffee largeCoffee = new LargeCoffee(milk);
        SmallCoffee smallCoffee = new SmallCoffee(sugar);
        largeCoffee.makeCoffee();
        smallCoffee.makeCoffee();
    }

    private void TestAidl() {
        /*调用自身*/
        /*
        // start service
        Intent startService = new Intent(MainActivity.this,ServiceTest.class);
        startService(startService);
        // stop service
        Intent stopService = new Intent(MainActivity.this,ServiceTest.class);
        stopService(stopService);
        // bind service
        Intent bindService = new Intent(MainActivity.this,ServiceTest.class);
        bindService(bindService,connectionSelf,BIND_AUTO_CREATE);
        // unbind service
        unbindService(connectionSelf);
        */

        /*进程间通信*/
        //bind
        Intent intent = new Intent();
        intent.setAction("com.pattern.design.service.MyService");
        //从 Android 5.0开始 隐式Intent绑定服务的方式已不能使用,所以这里需要设置Service所在服务端的包名
        intent.setPackage("com.pattern.design.service");
        bindService(intent, connectionAidl, BIND_AUTO_CREATE);
        // unbind
//        unbindService(connectionAidl);
    }


}
